package dao

import (
	"fmt"
	"hft/Http/Models/request"

	"gorm.io/gorm"
)

type BrokerHelp struct{}

func (b *BrokerHelp) PreloadInfo(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Preload("LouPanSwiper").Preload("Properties").Preload("SellStatus").Preload("SellLouPanInfo").Preload("Activities").Preload("Tags").Preload("RoomType").Preload("LouPanArea")
	}
}

func (b *BrokerHelp) Preload(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Preload("SellStatus").Preload("SellLouPanInfo")
	}
}

func (b *BrokerHelp) Search(paginate *request.BrokerHelpPage, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Scopes(b.LikeName(paginate.Name, db))
	}
}

func (b *BrokerHelp) LikeName(name string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("name LIKE ?", fmt.Sprintf("%%%v%%", name))
	}
}

func (b *BrokerHelp) GetLouPanList(paginate *request.BrokerHelpPage, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Scopes(b.Search(paginate, db)).Scopes(b.Preload(db)).Scopes(Paginate(Page(paginate.Page), db))
	}
}

var BrokerHelpDao = new(BrokerHelp)
